// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;

namespace MatrixFramework.Keycloak.Abstractions.Models;

/// <summary>
/// Realm创建结果
/// </summary>
public class RealmCreationResult
{
    /// <summary>
    /// Realm名称
    /// </summary>
    public string RealmName { get; set; } = string.Empty;

    /// <summary>
    /// Realm ID
    /// </summary>
    public string RealmId { get; set; } = string.Empty;

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; }

    /// <summary>
    /// 管理员用户名
    /// </summary>
    public string? AdminUsername { get; set; }

    /// <summary>
    /// 管理员密码（仅在创建时返回）
    /// </summary>
    public string? AdminPassword { get; set; }

    /// <summary>
    /// 管理控制台URL
    /// </summary>
    public string? AdminConsoleUrl { get; set; }

    /// <summary>
    /// 客户端配置信息
    /// </summary>
    public Dictionary<string, object>? ClientConfiguration { get; set; }
}

/// <summary>
/// 用户创建结果
/// </summary>
public class UserCreationResult
{
    /// <summary>
    /// 用户ID
    /// </summary>
    public string UserId { get; set; } = string.Empty;

    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; } = string.Empty;

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; }

    /// <summary>
    /// 临时密码（仅在创建时返回）
    /// </summary>
    public string? TemporaryPassword { get; set; }

    /// <summary>
    /// 是否需要更新密码
    /// </summary>
    public bool RequirePasswordUpdate { get; set; } = false;
}

/// <summary>
/// 角色创建结果
/// </summary>
public class RoleCreationResult
{
    /// <summary>
    /// 角色ID
    /// </summary>
    public string RoleId { get; set; } = string.Empty;

    /// <summary>
    /// 角色名称
    /// </summary>
    public string RoleName { get; set; } = string.Empty;

    /// <summary>
    /// 客户端ID
    /// </summary>
    public string? ClientId { get; set; }

    /// <summary>
    /// 容器ID
    /// </summary>
    public string? ContainerId { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; }
}

/// <summary>
/// 客户端创建结果
/// </summary>
public class ClientCreationResult
{
    /// <summary>
    /// 客户端ID
    /// </summary>
    public string ClientId { get; set; } = string.Empty;

    /// <summary>
    /// 客户端UUID
    /// </summary>
    public string ClientUuid { get; set; } = string.Empty;

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; }

    /// <summary>
    /// 客户端密钥（仅保密客户端返回）
    /// </summary>
    public string? ClientSecret { get; set; }

    /// <summary>
    /// 注册访问令牌（仅动态客户端返回）
    /// </summary>
    public string? RegistrationAccessToken { get; set; }
}

/// <summary>
/// 权限作用域创建结果
/// </summary>
public class ScopeCreationResult
{
    /// <summary>
    /// 作用域ID
    /// </summary>
    public string ScopeId { get; set; } = string.Empty;

    /// <summary>
    /// 作用域名称
    /// </summary>
    public string ScopeName { get; set; } = string.Empty;

    /// <summary>
    /// 客户端ID
    /// </summary>
    public string? ClientId { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; }
}

/// <summary>
/// 租户同步结果
/// </summary>
public class TenantSyncResult
{
    /// <summary>
    /// 租户ID
    /// </summary>
    public string TenantId { get; set; } = string.Empty;

    /// <summary>
    /// 租户名称
    /// </summary>
    public string TenantName { get; set; } = string.Empty;

    /// <summary>
    /// 同步时间
    /// </summary>
    public DateTime SyncTime { get; set; }

    /// <summary>
    /// 同步状态
    /// </summary>
    public SyncStatus Status { get; set; }

    /// <summary>
    /// 成功的操作列表
    /// </summary>
    public List<string> SuccessfulOperations { get; set; } = new();

    /// <summary>
    /// 失败的操作列表
    /// </summary>
    public List<SyncError> FailedOperations { get; set; } = new();

    /// <summary>
    /// 创建的Realm名称
    /// </summary>
    public string? CreatedRealm { get; set; }

    /// <summary>
    /// 创建的用户ID列表
    /// </summary>
    public List<string> CreatedUserIds { get; set; } = new();

    /// <summary>
    /// 创建的角色列表
    /// </summary>
    public List<string> CreatedRoles { get; set; } = new();

    /// <summary>
    /// 创建的客户端ID列表
    /// </summary>
    public List<string> CreatedClientIds { get; set; } = new();
}

/// <summary>
/// 批量租户同步结果
/// </summary>
public class BatchTenantSyncResult
{
    /// <summary>
    /// 批次ID
    /// </summary>
    public Guid BatchId { get; set; }

    /// <summary>
    /// 开始时间
    /// </summary>
    public DateTime StartTime { get; set; }

    /// <summary>
    /// 结束时间
    /// </summary>
    public DateTime? EndTime { get; set; }

    /// <summary>
    /// 总租户数
    /// </summary>
    public int TotalTenants { get; set; }

    /// <summary>
    /// 成功数量
    /// </summary>
    public int SuccessCount { get; set; }

    /// <summary>
    /// 失败数量
    /// </summary>
    public int FailedCount { get; set; }

    /// <summary>
    /// 单个租户同步结果列表
    /// </summary>
    public List<TenantSyncResult> TenantResults { get; set; } = new();

    /// <summary>
    /// 进度百分比
    /// </summary>
    public double ProgressPercentage => TotalTenants > 0 ? (double)(SuccessCount + FailedCount) / TotalTenants * 100 : 0;

    /// <summary>
    /// 是否完成
    /// </summary>
    public bool IsCompleted => EndTime.HasValue;

    /// <summary>
    /// 总耗时（秒）
    /// </summary>
    public double? DurationSeconds => EndTime.HasValue ? (EndTime.Value - StartTime).TotalSeconds : null;
}

/// <summary>
/// 租户同步状态
/// </summary>
public class TenantSyncStatus
{
    /// <summary>
    /// 租户ID
    /// </summary>
    public string TenantId { get; set; } = string.Empty;

    /// <summary>
    /// 租户名称
    /// </summary>
    public string TenantName { get; set; } = string.Empty;

    /// <summary>
    /// 同步状态
    /// </summary>
    public SyncStatus Status { get; set; }

    /// <summary>
    /// 最后同步时间
    /// </summary>
    public DateTime? LastSyncTime { get; set; }

    /// <summary>
    /// 进度百分比
    /// </summary>
    public int ProgressPercentage { get; set; }

    /// <summary>
    /// 当前操作
    /// </summary>
    public string? CurrentOperation { get; set; }

    /// <summary>
    /// 错误信息
    /// </summary>
    public string? ErrorMessage { get; set; }

    /// <summary>
    /// 重试次数
    /// </summary>
    public int RetryCount { get; set; }

    /// <summary>
    /// 预计完成时间
    /// </summary>
    public DateTime? EstimatedCompletionTime { get; set; }
}

/// <summary>
/// 同步错误
/// </summary>
public class SyncError
{
    /// <summary>
    /// 操作类型
    /// </summary>
    public string Operation { get; set; } = string.Empty;

    /// <summary>
    /// 错误代码
    /// </summary>
    public string? ErrorCode { get; set; }

    /// <summary>
    /// 错误消息
    /// </summary>
    public string ErrorMessage { get; set; } = string.Empty;

    /// <summary>
    /// 详细错误信息
    /// </summary>
    public string? Details { get; set; }

    /// <summary>
    /// 错误时间
    /// </summary>
    public DateTime ErrorTime { get; set; }

    /// <summary>
    /// 是否可重试
    /// </summary>
    public bool Retryable { get; set; } = true;
}

/// <summary>
/// 健康检查结果
/// </summary>
public class HealthCheckResult
{
    /// <summary>
    /// 是否健康
    /// </summary>
    public bool IsHealthy { get; set; }

    /// <summary>
    /// 检查时间
    /// </summary>
    public DateTime CheckTime { get; set; }

    /// <summary>
    /// 响应时间（毫秒）
    /// </summary>
    public long ResponseTimeMs { get; set; }

    /// <summary>
    /// 服务器版本
    /// </summary>
    public string? ServerVersion { get; set; }

    /// <summary>
    /// 服务器信息
    /// </summary>
    public ServerInfo? ServerInfo { get; set; }

    /// <summary>
    /// 检查项结果
    /// </summary>
    public Dictionary<string, HealthCheckItem> Checks { get; set; } = new();

    /// <summary>
    /// 总体状态
    /// </summary>
    public HealthStatus OverallStatus => IsHealthy ? HealthStatus.Healthy : HealthStatus.Unhealthy;
}

/// <summary>
/// 健康检查项
/// </summary>
public class HealthCheckItem
{
    /// <summary>
    /// 检查项名称
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 是否通过
    /// </summary>
    public bool Passed { get; set; }

    /// <summary>
    /// 响应时间（毫秒）
    /// </summary>
    public long ResponseTimeMs { get; set; }

    /// <summary>
    /// 状态消息
    /// </summary>
    public string? Message { get; set; }

    /// <summary>
    /// 详细信息
    /// </summary>
    public Dictionary<string, object>? Details { get; set; }
}

/// <summary>
/// 同步状态
/// </summary>
public enum SyncStatus
{
    /// <summary>
    /// 待同步
    /// </summary>
    Pending = 0,

    /// <summary>
    /// 同步中
    /// </summary>
    InProgress = 1,

    /// <summary>
    /// 成功
    /// </summary>
    Success = 2,

    /// <summary>
    /// 失败
    /// </summary>
    Failed = 3,

    /// <summary>
    /// 已取消
    /// </summary>
    Cancelled = 4,

    /// <summary>
    /// 部分成功
    /// </summary>
    PartialSuccess = 5
}

/// <summary>
/// 健康状态
/// </summary>
public enum HealthStatus
{
    /// <summary>
    /// 健康
    /// </summary>
    Healthy = 0,

    /// <summary>
    /// 不健康
    /// </summary>
    Unhealthy = 1,

    /// <summary>
    /// 降级
    /// </summary>
    Degraded = 2,

    /// <summary>
    /// 未知
    /// </summary>
    Unknown = 3
}

/// <summary>
/// 用户会话信息
/// </summary>
public class UserSessionInfo
{
    /// <summary>
    /// 会话ID
    /// </summary>
    public string Id { get; set; } = string.Empty;

    /// <summary>
    /// 用户ID
    /// </summary>
    public string UserId { get; set; } = string.Empty;

    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; } = string.Empty;

    /// <summary>
    /// 客户端ID
    /// </summary>
    public string ClientId { get; set; } = string.Empty;

    /// <summary>
    /// IP地址
    /// </summary>
    public string? IpAddress { get; set; }

    /// <summary>
    /// 开始时间
    /// </summary>
    public DateTime StartedAt { get; set; }

    /// <summary>
    /// 最后访问时间
    /// </summary>
    public DateTime LastAccessAt { get; set; }

    /// <summary>
    /// 过期时间
    /// </summary>
    public DateTime? ExpiresAt { get; set; }

    /// <summary>
    /// 是否活跃
    /// </summary>
    public bool IsActive { get; set; }

    /// <summary>
    /// 浏览器信息
    /// </summary>
    public string? UserAgent { get; set; }
}

/// <summary>
/// 用户导入结果
/// </summary>
public class UserImportResult
{
    /// <summary>
    /// 批次ID
    /// </summary>
    public Guid BatchId { get; set; }

    /// <summary>
    /// 总用户数
    /// </summary>
    public int TotalUsers { get; set; }

    /// <summary>
    /// 成功数量
    /// </summary>
    public int SuccessCount { get; set; }

    /// <summary>
    /// 失败数量
    /// </summary>
    public int FailedCount { get; set; }

    /// <summary>
    /// 跳过数量（已存在）
    /// </summary>
    public int SkippedCount { get; set; }

    /// <summary>
    /// 成功的用户列表
    /// </summary>
    public List<UserImportSuccessItem> SuccessItems { get; set; } = new();

    /// <summary>
    /// 失败的用户列表
    /// </summary>
    public List<UserImportErrorItem> ErrorItems { get; set; } = new();

    /// <summary>
    /// 导入时间
    /// </summary>
    public DateTime ImportedAt { get; set; }

    /// <summary>
    /// 成功率
    /// </summary>
    public double SuccessRate => TotalUsers > 0 ? (double)SuccessCount / TotalUsers * 100 : 0;
}

/// <summary>
/// 用户导出结果
/// </summary>
public class UserExportResult
{
    /// <summary>
    /// 搜索条件
    /// </summary>
    public UserSearchRequest SearchRequest { get; set; }

    /// <summary>
    /// 总用户数
    /// </summary>
    public int TotalUsers { get; set; }

    /// <summary>
    /// 导出用户数
    /// </summary>
    public int ExportedUsers { get; set; }

    /// <summary>
    /// 导出格式
    /// </summary>
    public ExportFormat Format { get; set; }

    /// <summary>
    /// 文件路径
    /// </summary>
    public string? FilePath { get; set; }

    /// <summary>
    /// 文件大小（字节）
    /// </summary>
    public long FileSizeBytes { get; set; }

    /// <summary>
    /// 导出时间
    /// </summary>
    public DateTime ExportedAt { get; set; }

    /// <summary>
    /// 下载链接（有效期24小时）
    /// </summary>
    public string? DownloadUrl { get; set; }

    /// <summary>
    /// 过期时间
    /// </summary>
    public DateTime? ExpiresAt { get; set; }
}

/// <summary>
/// 用户导入成功项
/// </summary>
public class UserImportSuccessItem
{
    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; } = string.Empty;

    /// <summary>
    /// 用户ID
    /// </summary>
    public string UserId { get; set; } = string.Empty;

    /// <summary>
    /// 邮箱
    /// </summary>
    public string Email { get; set; } = string.Empty;
}

/// <summary>
/// 用户导入错误项
/// </summary>
public class UserImportErrorItem
{
    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; } = string.Empty;

    /// <summary>
    /// 错误消息
    /// </summary>
    public string ErrorMessage { get; set; } = string.Empty;

    /// <summary>
    /// 错误代码
    /// </summary>
    public string? ErrorCode { get; set; }

    /// <summary>
    /// 行号
    /// </summary>
    public int? LineNumber { get; set; }
}

/// <summary>
/// 导出格式
/// </summary>
public enum ExportFormat
{
    /// <summary>
    /// CSV格式
    /// </summary>
    Csv = 1,

    /// <summary>
    /// JSON格式
    /// </summary>
    Json = 2,

    /// <summary>
    /// Excel格式
    /// </summary>
    Excel = 3
}